package com.iplatform.file.function.resourcedata.action;

import com.iplatform.commons.exception.CustomException;
import com.iplatform.commons.utils.CommResponse;
import com.iplatform.file.function.front.vo.TreeSelectVo;
import com.iplatform.file.function.resourcedata.common.ResourceDataConstants;
import com.iplatform.file.function.resourcedata.entity.MenuBean;
import com.iplatform.file.function.resourcedata.feign.UserClient;
import com.iplatform.file.function.resourcedata.service.MenuService;
import com.iplatform.module.privilege.PriMenuBean;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * @className: MenuController
 * @description: TODO 类描述
 * @author: chensk
 * @date: 2020/12/3
 **/
@RestController
@RequestMapping("/menu")
@Api(tags = "数据资源管理目录板块")
public class MenuController {
	@Resource
	private MenuService menuService;
	
	
	@Autowired
	private UserClient userClient;
	
	@GetMapping("/selectAllMenu")
	@ApiOperation(value = "目录查询操作", notes = "所在包：com.iplatform.data.function.resource.controller 方法名：selectAllMenu")
	@ApiResponses({@ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
			@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范")})
	public ResponseEntity<CommResponse<TreeSelectVo>> selectAllMenu() {
		
		CommResponse<TreeSelectVo> commResponse = new CommResponse<>();
		commResponse.setResults(menuService.selectAllMenu());
		return new ResponseEntity<>(commResponse, HttpStatus.OK);
	}
	
	@GetMapping(value = "/selectChildByParentId")
	@ApiOperation(value = "子目录查询操作", notes = "所在包：com.iplatform.data.function.resource.controller 方法名：selectChildByParentId")
	@ApiResponses({@ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
			@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范")})
	public ResponseEntity<CommResponse<MenuBean>> selectChildByParentId(Long parentId) {
		//在新增目录时需要验证是否重复要用到menuName参数
		String menuName = null;
		List<MenuBean> result = menuService.selectChildByParentId(parentId, menuName);
		CommResponse<MenuBean> response = new CommResponse<>();
		response.setMsg(HttpStatus.OK.toString());
		response.setRes(Boolean.TRUE);
		response.setResults(result);
		return new ResponseEntity<>(response, HttpStatus.OK);
		
	}
	
	@GetMapping(value = "/selectById")
	@ApiOperation(value = "根据ID查询操作", notes = "所在包：com.iplatform.data.function.resource.controller 方法名：selectById")
	@ApiResponses({@ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
			@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范")})
	public ResponseEntity<CommResponse<MenuBean>> selectById(Long id) {
		
		List<MenuBean> result = menuService.selectById(id);
		CommResponse<MenuBean> response = new CommResponse<>();
		response.setMsg(HttpStatus.OK.toString());
		response.setRes(Boolean.TRUE);
		response.setResults(result);
		return new ResponseEntity<>(response, HttpStatus.OK);
	}
	
	@PostMapping(value = "/insertOrUpdate")
	@ApiOperation(value = "目录新增操作", notes = "所在包：com.iplatform.data.function.resource.controller 方法名：insertOrUpdate")
	@ApiResponses({@ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
			@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范")})
	public ResponseEntity<CommResponse<MenuBean>> insertOrUpdate(MenuBean menuBean) {
		List<MenuBean> result = menuService.insertOrUpdate(menuBean);
		CommResponse<MenuBean> response = new CommResponse<>();
		response.setMsg(HttpStatus.OK.toString());
		response.setRes(Boolean.TRUE);
		response.setResults(result);
		return new ResponseEntity<>(response, HttpStatus.OK);
		//return new ResponseEntity<>(new CommResponse<Integer>(HttpStatus.CREATED.toString(), Boolean.TRUE,result), HttpStatus.CREATED);
		
	}
	
	
	@DeleteMapping(value = "/delete")
	@ApiOperation(value = "目录删除操作", notes = "所在包：com.iplatform.data.function.resource.controller 方法名：delete")
	@ApiResponses({@ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
			@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范")})
	public ResponseEntity<CommResponse<Integer>> delete(Long id) {
		Integer result = menuService.deleteById(id);
		return new ResponseEntity<>(new CommResponse<Integer>(HttpStatus.NO_CONTENT.toString(), Boolean.TRUE, result), HttpStatus.NO_CONTENT);
		
	}
	
	
	@PutMapping(value = "/releaseMenu/{menuId}")
	@ApiOperation(value = "发布菜单")
	public ResponseEntity<CommResponse<Integer>> releaseMenu(@PathVariable Long menuId) {
		MenuBean menuBean = menuService.selectByMenuId(menuId);
		if (menuBean == null) {
			throw new CustomException("找不到你要发布的菜单");
		}
		PriMenuBean body = userClient.findMenuIdBySysDataCodeType(ResourceDataConstants.META_DATA_MENU_TYPE).getBody();
		if (body != null) {
			PriMenuBean priMenuBean = new PriMenuBean();
			priMenuBean.setMenuLink(body.getMenuLink() + "/" + ResourceDataConstants.RELEASE_MENU);
			priMenuBean.setMenuName(menuBean.getMenuName());
			priMenuBean.setParentId(body.getMenuId());
			priMenuBean.setMenuOrd(1);
			priMenuBean.setResourceMenuId(menuBean.getId() + "");
			userClient.add(priMenuBean);
			return new ResponseEntity<>(new CommResponse<>("发布成功，路径为" + body.getMenuLink() + "/" + ResourceDataConstants.RELEASE_MENU, Boolean.TRUE), HttpStatus.OK);
			
		} else {
			throw new CustomException("找不到要发布到哪个菜单上，请联系管理员配置");
		}
		
	}
	
	
	@GetMapping("/selectChildMenusByMenuId/{menuId}")
	@ApiOperation(value = "根据目录id查询子目录", notes = "所在包：com.iplatform.data.function.resource.controller 方法名：selectAllMenu")
	public ResponseEntity<CommResponse<TreeSelectVo>> selectChildMenusByMenuId(@ApiParam("目录id") @PathVariable Long menuId) {
		
		CommResponse<List<TreeSelectVo>> commResponse = new CommResponse<>();
		commResponse.setResult(menuService.selectChildMenuByMenuId(menuId));
		return new ResponseEntity(commResponse, HttpStatus.OK);
	}
	
	
}
